package errorx

import "fmt"

type CodeError struct {
	Code uint32 `json:"errorCode"`
	Msg  string `json:"errorMessage"`
}

type CodeErrorResponse struct {
	Success  bool   `json:"success"`
	Code     uint32 `json:"errorCode"`
	Msg      string `json:"errorMessage"`
	Data     any    `json:"data,omitempty"`
	ShowType uint32 `json:"showType"`
	TraceId  uint64 `json:"traceId,omitempty"`
	Host     string `json:"host,omitempty"`
}

func NewCodeError(code uint32, msg string) *CodeError {
	return &CodeError{Code: code, Msg: msg}
}

func NewDefaultError(msg string) *CodeError {
	return NewCodeError(ERROR_DEFAULT, msg)
}

func (e *CodeError) Error() string {
	return fmt.Sprintf("errorCode: %d, errorMessage: %s", e.Code, e.Msg)
}

func (e *CodeError) Data() *CodeErrorResponse {
	return &CodeErrorResponse{
		Success:  false,
		Code:     e.Code,
		Msg:      e.Msg,
		ShowType: 4,
	}
}
